/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.SmsSend;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.SmsSendRepository;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */

@Stateless
public class SmsSendPersistence implements SmsSendPersistenceLocal{

    private static final Logger logger = LoggerFactory.getLogger();

    @PersistenceContext
    private EntityManager em;
    private SmsSendRepository smsSendRepository;

    @PostConstruct
    public void initialize(){
        smsSendRepository = DAOFactory.getSmsSendRepository(em);
    }

    public void persist(SmsSend smsSend){
        smsSendRepository.persist(smsSend);
    }
    
    public void update(SmsSend smsSend){
        smsSendRepository.update(smsSend);
    }

    public void remove(SmsSend smsSend){
        smsSendRepository.remove(smsSend, smsSend.getPkSmsSend());
    }

    public Login findWhoSentLast(String phone){
        SmsSend smsSend = smsSendRepository.findWhoSentLastSms(phone);
        if(smsSend != null)
            return smsSendRepository.findWhoSentLastSms(phone).getLogin();
        else
            return null;
    }

    public SmsSend findByNowSmsId(String nowSmsId){
        return smsSendRepository.findByNowSmsId(nowSmsId);
    }

    public List<SmsSend> list(SmsSend smsSend){
        return smsSendRepository.list(smsSend);
    }

    public List<SmsSend> list(String name, String phone){
        SmsSend smsSend = new SmsSend();
        ClientPhone clientPhone = new ClientPhone();
        clientPhone.setName(name);
        clientPhone.setPhone(phone);
        
        smsSend.setClientPhone(clientPhone);
        return smsSendRepository.list(smsSend);
    }

    public List<SmsSend> listAll(){
        return smsSendRepository.listAll();
    }
}
